﻿#region NeoPatterns
// 
// NeoPatterns framework is released under LGPLv3 license.
// See http://www.opensource.org/licenses/lgpl-3.0.html for more details.
// 
// Please visit http://neopatterns.wikidot.com for more information about NeoPatterns
// 
// Copyright (c) Pascal Craponne 2008
// 
#endregion

using System.Collections.Generic;
using NeoPatterns.Core.Registry;

namespace NeoPatterns.Patterns.FactoryMethod
{
    public class ObjectScopeFactoryMethod<TValue, TResultInterface> : IFactoryMethod<TValue, TResultInterface>
    {
        public IObjectRegistry ObjectRegistry { get; private set; }

        public IDictionary<TValue, string> Values { get; private set; }

        /// <summary>
        /// Factory method: returns a concrete object from a given value
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public TResultInterface GetObject(TValue value)
        {
            return ObjectRegistry.Get<TResultInterface>(Values[value]);
        }

        public ObjectScopeFactoryMethod(IObjectRegistry objectRegistry, params FactoryDefinition<TValue>[] factoryDefinitions)
        {
            ObjectRegistry = objectRegistry;
            Values = new Dictionary<TValue, string>();
            foreach (var factoryDefinition in factoryDefinitions)
                Values[factoryDefinition.Value] = factoryDefinition.ConcreteObjectName;
        }
    }
}